/*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Alternatively, this software may be distributed under the terms of BSD
* license.
*
*
*
* Authors:                  zhoujian
* Email:            343600434@qq.com
* Date:         Mar-11-2024 17:15:42
*
*/


#ifndef __flash_H__
#define __flash_H__


#include <stdint.h>
#include "user_motor_drv.h"
#ifdef __cplusplus
extern "C" {
#endif

#ifndef FLASH_PAGE_SIZE
#define FLASH_PAGE_SIZE        ((uint16_t)0x1000)
#endif
//#define FLASH_WRITE_START_ADDR ((uint32_t)0x0807F800)
//#define FLASH_WRITE_END_ADDR   ((uint32_t)0x08080000)

#define ID_ADDR0 0x1FFFF7E8
#define ID_ADDR1 0x1FFFF7EC
#define ID_ADDR2 0x1FFFF7F0

#define BUF_SIZE (uint32_t)32


#define LINURM_DEBUG_FLASH_ERASE_BUG


/*
typedef enum {
    FLASH_State_IDLE = 0x00,
    FLASH_State_ERASE = 0x02,
    FLASH_State_EWAIT = 0x03,
    FLASH_State_UNLOCK = 0x04,
    FLASH_State_LOCK = 0x05,
    FLASH_State_WRITE = 0x06,
    FLASH_State_WCOMP = 0x07,
    FLASH_State_WWAIT = 0x08,
    FLASH_State_EWAIT2 = 0x09,
#ifdef LINURM_DEBUG_FLASH_ERASE_BUG
    FLASH_State_WWAITDMA = 0x0A,
#endif
    FLASH_State_WSTART,
} FLASH_zTypeDef;
*/

typedef enum {
    FLASH_IDLE,
    FLASH_READY,
    FLASH_ERASE_READY,
    FLASH_ERASE_OK,
    FLASH_WRITE_READY,
    FLASH_WRITE_READY2,
    FLASH_WRITE_OK,
    FLASH_END,
    FLASH_READY_C,
    FLASH_ERASE_READY_C,
    FLASH_ERASE_OK_C,
    FLASH_WRITE_READY_C,
    FLASH_WRITE_READY2_C,
    FLASH_WRITE_OK_C,
} FLASH_STATUS;


typedef struct {
    uint8_t status;
    //uint8_t int_flag;
    uint8_t timeout;
    uint16_t addr;
    uint16_t index;
    uint16_t len;
    uint32_t* dat;
} FLASH_T;

extern FLASH_T flash_st;

//0x0803 F000 - 0x0803 FFFF 4KB
#define SAVE_ADDR 0x803F000
#define FLASH_WRITE_START_COUNT_ADDR ((uint32_t)0x803E000)


//extern FLASH_zTypeDef flash_state;

#ifdef COUNT_CARD_TEST

void read_count_from_flash(void);
void write_count_to_flash(uint32_t counter);

#endif

void flash_init(void);
void flash_task(void);
void flash_deinit(void);
void read_from_flash(void);
void flash_reset(void);

void write_to_flash(void);

void restore_menu_settings(void);



void save_settings_to_flash(void);


extern uint32_t id[3];

#ifdef __cplusplus
}
#endif

#ifdef __cplusplus
class flash
{

public:
    flash();
    ~flash();

};
#endif



#endif
